package DetailedLock;

import java.util.concurrent.TimeUnit;

/**
 * @projectName: JUC
 * @package: DetailedLock
 * @className: DeadLockDemo
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/5 2:37
 * @version: 1.0
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        String a = "lockA";
        String b = "lockB";
        new Thread(new mythread(a, b), "t1").start();
        new Thread(new mythread(b, a), "t2").start();
    }
}

class mythread implements Runnable {
    private String a;
    private String b;

    public mythread(String a, String b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public void run() {
        synchronized (a) {
            System.out.println(Thread.currentThread().getName() + "lock=>" + a + "lock=>" + b);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (b) {
                System.out.println(Thread.currentThread().getName() + "lock=>" + b + "lock=>" + a);
            }
        }
    }
}
